package com.example.com.skills_utf8;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.google.gson.FieldAttributes;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class _57Activity extends Activity{

	
	final int CHOOSE_BIG_PICTURE = 1000;
	final int CHOOSE_SMALL_PICTURE = 1001;
	
	
	private Uri imageUri;

	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_57);
		
		/*
		Button btn_select = (Button)findViewById(R.id.btn_select);
		btn_select.setOnClickListener(new OnClickListener() {
			
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String packageName = getPackageName();
				File sdCardpath = Environment.getExternalStorageDirectory();
				File image = new File(sdCardpath.getPath(), "1.jpg");// packageName+
				try {
					image.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				imageUri = Uri.fromFile(image);
				
				System.out.println("imageUri: "+imageUri);
				
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
				intent.setType("image/*");
				intent.putExtra("crop", "true");
				intent.putExtra("aspectX", 1);
				intent.putExtra("aspectY", 1);
				intent.putExtra("outputX", 300);
				intent.putExtra("outputY", 300);
				intent.putExtra("scale", true);
				intent.putExtra("return-data", false);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
				intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
				intent.putExtra("noFaceDetection", true); // no face detection
				startActivityForResult(intent, CHOOSE_BIG_PICTURE);
				
			}
		});
		*/
		
		
		
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case CHOOSE_BIG_PICTURE:
			if (imageUri != null) {
				Bitmap bitmap = decodeUriAsBitmap(imageUri);// decode bitmap
				
				startPhotoZoom(imageUri);
				
			}
			break;
		case CHOOSE_SMALL_PICTURE:
			if (data != null) {
				Bitmap bitmap = data.getParcelableExtra("data");
				//imageView.setImageBitmap(bitmap);
			} else {
				System.out.println("CHOOSE_SMALL_PICTURE: data = " + data);
			}
			break;
			
		case 4000:
			System.out.println("得打剪切数据");
			data.getParcelableExtra("data");
			break;
		default:
			break;
		}
	
	}
	
	private Bitmap decodeUriAsBitmap(Uri uri){
		 Bitmap bitmap = null;
		 try {
		  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
		 } catch (FileNotFoundException e) {
		  e.printStackTrace();
		  return null;
		 }
		 return bitmap;
		}
	
	
	

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 320);
        intent.putExtra("outputY", 320);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 400);
    }
    
	
	
}
